#include "Door.h"
#include "Game.h"
#include "StringLib.h"
#include "Renderer.h"
#include "Util.h"

Door::Door(LockType lockType, bool locked) : LockedObstacle(GT_OB_DOOR, lockType, locked) {
	m_open = false;
	m_blocking = true;
}

Door::Door(std::ifstream& file) : LockedObstacle(GT_OB_DOOR, file) {
	int* tokens = Util::intSplit(file);
	m_open = tokens[0] == 1;
	m_blocking = !m_open;
	delete[] tokens;
}

Door::~Door() {
}

void Door::save(std::ofstream& file) const {
	file << getType(TH_GENERIC_TYPE) << "\n";
	LockedObstacle::save(file);
	file << (m_open ? 1 : 0) << "\n";
}

void Door::render(float x, float y) {
	g_renderer->render(*this, x, y);
}

std::string Door::getName() const {
	if(m_open)
		return "open " + getBasicName();
	if(m_lockType != LT_NONE)
		return std::string(m_locked ? "locked " : "unlocked ") + getBasicName();
	return "closed " + getBasicName();
}

void Door::logDetails(TextLog& log) const {
	Obstacle::logDetails(log);
	log.addBlankLine();
	if(m_lockType != LT_NONE) {
		log.addLine(g_stringLib->getTextString(ST_DOOR_LOCK_TYPE, g_stringLib->getMiscString(MST_LOCK, m_lockType).c_str()));
		log.addLine(g_stringLib->getTextString(ST_DOOR_LOCKED, std::string(m_locked ? "locked" : "unlocked").c_str()));
	}
	log.addLine(g_stringLib->getTextString(ST_DOOR_OPEN, std::string(m_open ? "open" : "closed").c_str()));
}

bool Door::toggleLock() {
	if(!m_locked && m_open)
		return false;
	return LockedObstacle::toggleLock();
}
